﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using Microsoft.SqlServer.Management.Smo;
using Microsoft.SqlServer.Management.Common;

namespace XEvents.Library
{
    public class SqlServer
    {
        #region Private Members

        private Server server;
        private Internal.MetaData metadata;
        internal Internal.EventSessionData sessiondata;

        #endregion

        #region Properties

        public override string ToString()
        {
            return ServerName;
        }

        internal string ConnectionString
        {
            get { return server.ConnectionContext.ConnectionString; }
        }

        public string ServerName
        {
            get { return server.Name; }
        }

        public System.Xml.XmlDocument GetSessionXml()
        {
            return Utilities.GetSerializedXml(typeof(EventSessionCollection), this.EventSessions);
        }


        internal ServerConnection ServerConnection
        {
            get { return server.ConnectionContext; }
        }

        /// <summary>
        /// Extended Event Engine MetaData for this Server
        /// </summary>
        public Internal.MetaData MetaData
        {
            get { return metadata; }
        }


        public EventSessionCollection EventSessions
        {
            get
            {
                return new EventSessionCollection(from ses in sessiondata.server_event_sessions
                       select new EventSession(ses, this));
            }
        }


        #endregion

        public void Refresh()
        {
            metadata = new XEvents.Library.Internal.MetaData(this);
            sessiondata = new XEvents.Library.Internal.EventSessionData(this);
        }

        private void ValidateConnection()
        {
            if (!(server.VersionMajor == 10))
            {
                throw new Exception("Extended Events are only Available on SQL 2008");
            }

            if (!server.ConnectionContext.IsInFixedServerRole(FixedServerRoles.SysAdmin))
            {
                throw new Exception("The specified User is not a member of the sysadmin fixed Server Role.\r\n  Managing Extended Events requires elevated permissions in the SQL Server.  Please specify another user or add this user to the sysadmin role and try again.");
            }
            Refresh();
        }



        #region Constructors

        public SqlServer(string servername)
        {
            try
            {
                server = new Server(servername);
                ValidateConnection();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public SqlServer(string servername, string username, string password)
        {
            ServerConnection conn = new ServerConnection(servername, username, password);
            try
            {
                server = new Server(conn);

                ValidateConnection();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        #endregion
    }
}
